package com.xd.cloud.service.core;

import com.xd.cloud.dao.Page;
import com.xd.cloud.dao.core.MenuDao;
import com.xd.cloud.entity.core.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wangtao
 * @class_name MenueService
 * @description 菜单service业务层
 * @create 2017/12/19
 */
@Service
public class MenueService  {
    @Autowired
    private MenuDao menuDao;


    public Page getAllMenu(int sindex, int ps) {
        return  menuDao.getAllMenu(sindex,ps);
    }

    public List getMenuTree() {
        List list = new ArrayList();
        Map map = null;
        List<Menu> parentMenu = menuDao.getParentMenu();
        for (Menu menu: parentMenu) {
            map = new HashMap();
            map.put("menu",menu);
            List  _list = menuDao.getChildrenMenu(menu.getId());
            map.put("children",_list);
            list.add(map);
            map = null;
        }
        return list;
    }

    public Menu saveMenu(Menu menu) {
        menuDao.save(menu);
        return  menu;
    }

    public Menu getMenueById(Integer menuid) {
        Menu menu = menuDao.get(menuid);
        return  menu;
    }

    public void updateMenu(Menu oldMenu) {
        menuDao.update(oldMenu);
    }

    public void delMenu(Menu menu) {
        menuDao.delete(menu);
    }

    public List<Menu> getMenuesByPId(Integer parentid) {
        return menuDao.getChildrenMenu(parentid);
    }

    public void orderMenuByPids(Integer[] ids) {
        for(int i = 0 ;i<ids.length;i++){
            Integer id = ids[i];
            Menu menu = menuDao.get(id);
            menu.setPorder(i);
            menuDao.update(menu);
        }
    }

    public void orderchildMenuByids(Integer[] ids) {
        for(int i = 0 ;i<ids.length;i++){
            Integer id = ids[i];
            Menu menu = menuDao.get(id);
            menu.setCorder(i);
            menuDao.update(menu);
        }
    }

    public List<Menu> findMenuByModule( String module) {
        return menuDao.findMenuByModule( module);
    }

    public List<Menu> findMenuBySubModule( String module) {
        return menuDao.findMenuBySubModule(module);
    }

}
